<?php

namespace App\Http\Controllers\Api\V1;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class PriceListController extends BaseController
{
    /**
     * 下单第一次请求, 生成订单号返回给前端
     */
    public function store(Request $request, $spid, $termid)
    {
        // 这里打日志断点记录参数信息, 是为了调试接口
        Log::info('保存订单接口:', [
            // 请求信息
            'request_info' => [
                // 请求IP
                'IP'     => $request->getClientIp() ? $_SERVER['REMOTE_ADDR'] : $request->getClientIp(),
                // 请求方式
                'method' => $request->getMethod(),
                // 请求的参数
                'params' => $request->all()
            ]
        ]);
        /**
         * 前端请求数据
         */
        $inputs  = $request->all();
        $content = [];
        // 判断参数不能为空
        if (empty($inputs['sp_name'])) {
            $content = ['status' => '404', 'data' => '服务商名为空!'];
        } elseif (empty($inputs['sp_title'])) {
            $content = ['status' => '404', 'data' => 'sp_title为空!'];
        } elseif (empty($inputs['pay_amount'])) {
            $content = ['status' => '404', 'data' => '订单金额为空!'];
        } elseif (empty($inputs['price_type'])) {
            $content = ['status' => '404', 'data' => '定价类型为空!'];
        } elseif (empty($inputs['mac'])) {
            $content = ['status' => '404', 'data' => '机顶盒号为空!'];
        } elseif (empty($inputs['card_id'])) {
            $content = ['status' => '404', 'data' => '智能卡ID为空!'];
        } elseif (empty($inputs['content_id'])) {
            $content = ['status' => '404', 'data' => '内容ID为空!'];
        } elseif (empty($inputs['service_code'])) {
            $content = ['status' => '404', 'data' => '服务code为空!'];
        } elseif (empty($inputs['service_type'])) {
            $content = ['status' => '404', 'data' => '服务类型为空!'];
        } else {
            // 拼装数据写入数据库
            $data = [];
            // 服务商名称
            $data['sp_name'] = $inputs['sp_name'];
            // 生成订单编号
            $data['order_no'] = $inputs['sp_title'] . date('YmdHis') . uniqid();
            // 订单金额
            $data['amount'] = $inputs['pay_amount'];
            // 支付结果
            // $data['pay_state'] = $inputs['pay_state'];
            // 媒资名称
            if (!empty($inputs['vasseti_name'])) {
                $data['vasseti_name'] = $inputs['vasseti_name'];
            }
            // 定价类型
            $data['price_type'] = $inputs['price_type'];
            // 机顶盒号
            $data['mac'] = $inputs['mac'];
            // 智能卡ID
            $data['card_id'] = $inputs['card_id'];
            // 内容ID
            $data['content_id'] = $inputs['content_id'];
            // 服务code
            $data['service_code'] = $inputs['service_code'];
            // 服务类型
            $data['service_type'] = $inputs['service_type'];
            // 创建时间
            $data['created_at'] = date("Y-m-d H:i:s");
            // 订单数据写入数据库
            $result = DB::table('orders')->insert($data);
            if ($result) {
                $content = ['status' => '200', 'data' => array_merge($inputs, ['order_no' => $data['order_no']])];
            } else {
                $content = ['status' => '404', 'data' => "订单保存失败!"];
            }
        }
        // 记录请求的信息, 和处理完之后的信息
        Log::info('保存订单接口:', [
            'request_info'  => [
                // 请求IP
                'IP'     => $request->getClientIp() ? $_SERVER['REMOTE_ADDR'] : $request->getClientIp(),
                // 请求方式
                'method' => $request->getMethod(),
                // 请求的参数
                'params' => $request->all()
            ],
            'response_info' => $content
        ]);
        return response()->json($content);
    }

    /**
     * 下单第二次请求, 更新订单信息记录
     */
    public function update(Request $request, $spid, $termid)
    {
        // 这里打日志断点记录参数信息, 是为了调试接口
        Log::info('更新订单接口:', [
            // 请求信息
            'request_info' => [
                // 请求IP
                'IP'     => $request->getClientIp() ? $_SERVER['REMOTE_ADDR'] : $request->getClientIp(),
                // 请求方式
                'method' => $request->getMethod(),
                // 请求的参数
                'params' => $request->all()
            ]
        ]);
        $inputs  = $request->all();
        $content = [];
        // 前端参数不为空判断
        if (empty($inputs['order_no'])) {
            $content = ['status' => '404', 'data' => '订单编号为空!'];
        } elseif (empty($inputs['result'])) {
            $content = ['status' => '404', 'data' => '订购结果为空!'];
        } elseif (empty($inputs['pay_state'])) {
            $content = ['status' => '404', 'data' => '支付结果为空!'];
        } else {
            // 判断订单号是否存在
            $count = DB::table('orders')->where('order_no', $inputs['order_no'])->count();
            if ($count) {
                // 拼装数据
                $data = [];
                // 订购结果
                $data['result'] = $inputs['result'];
                // 支付结果
                $data['pay_state'] = $inputs['pay_state'];
                // 失败的原因说明
                if (!empty($inputs['description'])) {
                    $data['description'] = $inputs['description'];
                }
                // 更新时间
                $data['updated_at'] = date('Y-m-d H:i:s');
                // 订单数据写入数据库
                $result = DB::table('orders')->where('order_no', $inputs['order_no'])->update($data);
                if ($result) {
                    $content = ['status' => '200', 'data' => "订单保存成功!"];
                } else {
                    $content = ['status' => '404', 'data' => "订单保存失败!"];
                }
            } else {
                $content = ['status' => '404', 'data' => '订单号不存在!'];
            }
        }
        // 记录请求的信息, 和处理完之后的信息
        Log::info('更新订单接口:', [
            'request_info'  => [
                // 请求IP
                'IP'     => $request->getClientIp() ? $_SERVER['REMOTE_ADDR'] : $request->getClientIp(),
                // 请求方式
                'method' => $request->getMethod(),
                // 请求的参数
                'params' => $request->all()
            ],
            'response_info' => $content
        ]);
        return response()->json($content);
    }

    /*
     * 订单保存
     */
    public function guizhoustore(Request $request, $spid, $termid)
    {
        // ZTEGC93FFA9E
        // 请求的数据
        $user_id = $request->input('user_id');
        // 订单ID
        $order_id = $request->input('order_id');
        // 定价ID
        $price_id = $request->input('price_id');
        // 金额
        $money = $request->input('money');
        // 订单状态
        $order_state = $request->input('order_state');
        // 影片ID
        $video_id = $request->input('video_id');
        // 视频名称
        $video_name = $request->input('video_name');
        // 支付渠道
        $payment = $request->input('payment');
        // 原因
        $reason = $request->input('reason');

        if (empty($spid)) {
            return response()->json(['status' => '404', 'data' => '服务商ID不能为空']);
        }
        if (empty($termid)) {
            return response()->json(['status' => '404', 'data' => '设备ID为空!']);
        }
        if (empty($user_id)) {
            return response()->json(['status' => '404', 'data' => '用户ID为空']);
        }
        if (empty($order_id)) {
            return response()->json(['status' => '404', 'data' => '订单ID为空!']);
        }
        if (empty($price_id)) {
            return response()->json(['status' => '404', 'data' => '定价ID为空!']);
        }
        if (empty($money)) {
            return response()->json(['status' => '404', 'data' => '金额为空!']);
        }
        if (empty($payment)) {
            return response()->json(['status' => '404', 'data' => '支付渠道为空!']);
        }
        $data                = [];
        $data['user_id']     = $user_id;
        $data['order_id']    = $order_id;
        $data['price_id']    = $price_id;
        $data['money']       = $money / 100;
        $data['order_state'] = $order_state;
        $data['payment']     = $payment;
        $data['created_at']  = date("Y-m-d H:i:s");
        if (!empty($video_id)) {
            $data['video_id'] = $video_id;
        }
        if (!empty($video_name)) {
            $data['video_name'] = $video_name;
        }
        if (!empty($reason)) {
            $data['reason'] = $reason;
        }

        // 请求类型
        $type       = "post";
        $url        = "http://10.216.90.21:8001/pricelist/store";
        $cmsResults = proxy_http_request($type, $url, $data);
        $results    = json_decode($cmsResults, 1);

        if ($results['status'] === 'error') {
            return response()->json(['status' => '404', 'data' => $results['content']]);
        }
        if ($results['status'] === 'success') {
            return response()->json(['status' => '200', 'data' => '订单保存成功']);
        }
    }

    /**
     * 订单删除
     */
    public function delete(Request $request)
    {
    }

    /**
     * 订单展示
     */
    public function show(Request $request)
    {
    }

}
